package no.progark.golf.controller;

import no.progark.golf.Golf;
import no.progark.golf.R;
import no.progark.golf.activities.GameSettingsActivity;
import no.progark.golf.activities.MainMenuActivity;
import no.progark.golf.activities.OptionsActivity;
import no.progark.golf.activities.TutorialActivity;
import android.content.Intent;
import android.app.Activity;
import android.content.Context;
import android.widget.ImageButton;
import android.sax.StartElementListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
public class MainMenuController {
	
	private static ImageButton newGameButton;
	private static ImageButton tutButton;
	private static ImageButton optionsButton;
	private static ImageButton exitButton;
	private static Context context;
	private static Activity activity;
	private static Intent svc;
	
	
	public static void setContext(Context c){
		MainMenuController.context = c;
		MainMenuController.activity = (Activity) c;

	}
	
	
	public static void setService(){
		Intent svc = new Intent(activity, BackgroundService.class);		
		MainMenuController.svc = svc;
	}
	
	public static void setMusic(){
		if(Golf.getInstance().isMusicEnabled())context.startService(svc);
		else {
			context.stopService(svc);
		}
	}
	
	//newGameButton
	
	public static void setNewGameButton(ImageButton b){
		MainMenuController.newGameButton = b;
		newGameButton.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN ) {
		            newGameButton.setImageResource(R.drawable.playbuttonpressed);

		        }
				if (event.getAction() == MotionEvent.ACTION_UP ) {
		            newGameButton.setImageResource(R.drawable.playbutton);

		        }
				return false;
			}
		});
		
		newGameButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MainMenuController.context, GameSettingsActivity.class);
				MainMenuController.context.startActivity(i);
			}
			
		
		});
	}

	
	
	public static void setTutButton(ImageButton b){
		MainMenuController.tutButton = b;
		tutButton.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN ) {
		            tutButton.setImageResource(R.drawable.tutbuttonpressed);

		        }
				if (event.getAction() == MotionEvent.ACTION_UP ) {
		            tutButton.setImageResource(R.drawable.tutbutton);

		        }
				return false;
			}
		});
		
		tutButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MainMenuController.context, TutorialActivity.class);
				MainMenuController.context.startActivity(i);
				
			}
			
		
		});
	}
	
	public static void setOptionsButton(ImageButton b){
		MainMenuController.optionsButton = b;
		optionsButton.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN ) {
					optionsButton.setImageResource(R.drawable.optionsbuttonpressed);

		        }
				if (event.getAction() == MotionEvent.ACTION_UP ) {
					optionsButton.setImageResource(R.drawable.optionsbutton);

		        }
				return false;
			}
		});
		optionsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MainMenuController.context, OptionsActivity.class);
				MainMenuController.context.startActivity(i);
			}
			
		
		});
	}
	
	public static void SetExitGameButton(ImageButton b){
		MainMenuController.exitButton = b;
		exitButton.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == MotionEvent.ACTION_DOWN ) {
					exitButton.setImageResource(R.drawable.exitbuttonpressed);

		        }
				if (event.getAction() == MotionEvent.ACTION_UP ) {
					exitButton.setImageResource(R.drawable.exitbutton);
					activity.moveTaskToBack(true);

		        }
				return false;
			}
		});
		exitButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				context.stopService(svc);
				MainMenuController.activity.finish();
			}
			
		
		});
	}
}
